package com.song.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @Author Klys
 * @Date 2023/5/28 20:06
 * @Description 通过三种方式获取用户信息
 * @Version 1.0
 */
@RestController
@Slf4j
public class CurrentLoginUserController {

    @GetMapping("getLoginUser1")
    public Authentication getLoginUser1(Authentication authentication) {
        return authentication;
    }

    @GetMapping("getLoginUser2")
    public Principal getLoginUser2(Principal principal) {
        return principal;
    }

    @GetMapping("getLoginUser3")
    public Principal getLoginUser3() {
        //通过上下文持有器获取安全上下文，在获取认证信息
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        return authentication;
    }

}
